package com.kukukk.kfkdroid;

import android.app.Activity;
import android.hardware.Camera;
import android.hardware.Camera.Parameters;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;

public class KfkDroidActivity extends Activity {
	private Camera mCamera;
	private Boolean mFlashlight;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        setContentView(R.layout.main);
        
        this.mFlashlight = false;
        
        final ImageButton button = (ImageButton) findViewById(R.id.imageButton);
        button.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                if(mCamera != null){
                    Parameters params = mCamera.getParameters();
                	if (mFlashlight) {
                        params.setFlashMode(Parameters.FLASH_MODE_OFF);
                        button.setImageResource(R.drawable.flashlight_off);
                        mFlashlight = false;
                	} else {
                        params.setFlashMode(Parameters.FLASH_MODE_TORCH);
                        button.setImageResource(R.drawable.flashlight_on);
                        mFlashlight = true;
                    }
                    mCamera.setParameters(params);
                }
            }
        });
    }
    
    @Override
    public void onResume() {
       super.onResume();
       
       try{
           this.mCamera = Camera.open();
       } catch( Exception e ){
       }
    }

    @Override
    public void onPause() {
       super.onPause();

       if(this.mCamera != null){
           this.mCamera.release();
           this.mCamera = null;
       }
       this.mFlashlight = false;
    }

    @Override
    public void onDestroy() {
       super.onDestroy();
    }
}